package com.example.gzysmartcity.utils;

import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class NetworkUtil {
    private static final class S {
        private static final NetworkUtil h = new NetworkUtil();
    }

    public static NetworkUtil getInstance() {
        return S.h;
    }

    private NetworkUtil() {
    }

    public static final Gson G = new Gson();
    /**
     * RequestBody requestBodyJson = RequestBody.create
     * (MediaType.parse("application/json; charset=utf-8"), jsonObject.toString())
     */
    public static final MediaType M = MediaType.parse("application/json;charset=utf-8");
    public static String base = "http://smartcity.lenovoedu.com/interface/";
    public static String userId = null;
    private static final OkHttpClient O = new OkHttpClient();

    public <T> T get(String url, RequestBody requestBody, Class<T> tClass, int i) throws IOException {
        url = base + url + "&userId=" + NetworkUtil.userId;
        Log.i("TAG", "get: " + url);
        if (i == 0)
            return G.fromJson(O.newCall(new Request.Builder().url(url).get().build()).execute().body().string(), tClass);
        if (i == 1)
            return G.fromJson(O.newCall(new Request.Builder().url(url).post(requestBody).build()).execute().body().string(), tClass);
        return null;
    }
}
